﻿@using Newtouch.Infrastructure
@{
	Layout = "~/Views/Shared/_Index.cshtml";

	var isZFtoYB = SysConfigReader.Bool("Inpatient_Open_ZF_to_YB", false).Value;

	//是否和医保交易
	var openYbSett = SysConfigReader.Bool("Inpatient_Sett_OpenYbSett");
	//医保所属地，区分系统将执行何处医保逻辑
	var medicalInsurance = SysConfigReader.String("Inpatient_MedicalInsurance");
	//HIS厂商编号
	var his_manufacturerID = SysConfigReader.String("HIS_ManufacturerID");

	var opr = Newtouch.Common.Operator.OperatorProvider.GetCurrent();
}

<style type="text/css">
	* {
		font-size: 20px;
	}

	ol {
		margin-top: 20px;
	}

	li i {
		margin-left: 10px;
	}

		li i.passing {
			color: #0FA74F;
		}

		li i.failed {
			color: red;
		}
</style>

<ol>
	<li><span>住院患者信息初步判断</span></li>
	@*<li><span>尚未开立非医保相关医嘱</span></li>*@
	<li><span>医保环境检查</span></li>
	<li><span>HIS唯一身份验证</span></li>
	<li><span>医保交互.就诊登记</span></li>
	<li><span>更新HIS.自费转医保</span></li>
	<li><span>同步数据至医生站</span></li>
	<li><span>成功</span></li>
</ol>

<script type="text/javascript">
	$(function () {
		go();

	});

	//递归调用each step
	function go(stepFuncName) {
		if (!!!stepFuncName) {
			stepFuncName = "funcStep" + 1;
			//stepFuncName = "funcStep" + 8;
		}
		setTimeout(function () {
			var func = window[stepFuncName];
			if (!!!func) {
				console.log('step function not found');
				return;
			}
			var indx = parseInt(stepFuncName.substring("funcStep".length));
			var $iTemp = $('<i>...executing...</i>');
			$iTemp.appendTo($('ol li:eq(' + (indx - 1) + ')'));
			var callback = function (isStepSuccess) {
				$iTemp.remove();
				if (isStepSuccess === true) {
					$('ol li:eq(' + (indx - 1) + ')').append('<i class="fa fa-check passing"></i>');
					//继续调用下一step function
					if ($('ol li').length > indx) {
						go("funcStep" + (indx + 1));
					}
				}
				else {
					$('ol li:eq(' + (indx - 1) + ')').append('<i class="fa fa-times failed"></i>');
				}
			};
			var flag = func(callback);
			if (flag === true || flag === false) {
				//同步返回模式
				callback(flag);
			}
			else {
				//回调模式
			}
		}, 50);
	}

</script>
<script type="text/javascript">

	var openYbSett = '@openYbSett' === 'True'; //开关配置：医保患者是否使用医保交易流程
	var medicalInsurance = '@medicalInsurance';
    var ybkCardType = "@((int)EnumCardType.YBJYK)";
    //var klx = $.request('klx');
    var mzh = $.request('mzh');
    var dkfs = $.request('dkfs');
    var pzm = $.request('pzm');
	//医保卡信息
	var cardInfo;
	//系统病人主键
	var patid;
	var accountInfo;
	var cqcardInfo;
	var patInfo;
	var ghxx;
    var xtbrxx;
    var yllb="1";
	var ryzd1;
	var rydjksmc;
	var rygjysbmobj;
	var cardInfo1;
	var cardInfo2;
	var cardInfo3;
	var yibaoCardInfo;
	var cqPatInfo = {};//患者读卡信息
	//入院办理信息（医保接口返回）
	var ryblInfo;
	var jzid;
	var errmsg = "";

	function funcStep1(nextStepCallback) {
		if (!!!mzh) {
			$.modalAlert('缺少参数：mzh', 'error');
			return false;
		}
		$.najax({
			url: "/OutpatientManage/OutpatCharge/ZFToYB_Step_1",
			data: { mzh: mzh },
			success: function (data) {
				nextStepCallback(true);
			},
			errorCallback: function () {
				nextStepCallback(false);
			},
			error: function () {
				nextStepCallback(false);
			}
		});
		return;
	}

	//function funcStep2(nextStepCallback) {
	//    $.najax({
	//        url: "/PatientManage/Inpatient/ZFToYB_Step_2",
	//        data: { mzh: mzh },
	//        success: function (data) {
	//            nextStepCallback(true);
	//        },
	//        errorCallback: function () {
	//            nextStepCallback(false);
	//        },
	//        error: function () {
	//            nextStepCallback(false);
	//        }
	//    });
	//    return;
	//}

	function funcStep2(nextStepCallback) {
	    $.loading(true, '正在读卡...');
	    var dk;
	    debugger;
	    if (medicalInsurance == "shanghai")
	    {
	        dk = shanghaiyb();
	    }
	    else if (medicalInsurance == "tongyong")
	    {
	        dk = gjyb();
	    }
	    if (dk.success) {
	        nextStepCallback(true);
	    }
	    else {
	        $.loading(false);
	        $.modalAlert(dk.msg, "error");
	        nextStepCallback(false);
	    }
	}
   

	function funcStep3(nextStepCallback) {
		$.najax({
			url: "/OutpatientManage/OutpatCharge/ZFToYB_Step_3",
			data: { mzh: mzh, sbbh: cqPatInfo.sbkh, xm: cqPatInfo.xm },
			success: function (data) {
				patid = data.data.patid;
				ghxx = data.data.ghxx;
                xtbrxx = data.data.xtbrxx;
                yllb = data.data.yllb;
                rygjysbmobj = data.data.rygjysbm;
				nextStepCallback(true);
			},
			errorCallback: function () {
			    $.loading(false);
				nextStepCallback(false);
			},
			error: function () {
			    $.loading(false);
				nextStepCallback(false);
			}
		});
		return;
	}

	function funcStep4(nextStepCallback) {
	    debugger;
		$.loading(true, '正在医保就诊登记...');
		setTimeout(function () {
		    var ghxx = {
		        psn_no: cqPatInfo.grbh,
		        insutype: cqPatInfo.xzlx,
		        begntime: $.getDate(),
		        mdtrt_cert_type: yibaoCardInfo.readCardCardType,
		        mdtrt_cert_no: yibaoCardInfo.kh,
		        ipt_otp_no: mzh,
		        atddr_no: rygjysbmobj.gjybdm,
		        dr_name: rygjysbmobj.name,
		        dept_code: rygjysbmobj.ybksbm,
		        dept_name: rygjysbmobj.ksmc,
		        caty: rygjysbmobj.ybksbm,
		        operatorId: '@(opr.rygh)',
		        orgId:'@(opr.OrganizeId)',
		        operatorName: '@(opr.UserName)',
		        insuplc_admdvs: cqPatInfo.xzqh,
		        hisId: mzh, djlx: "mz",
		        cardtype: cqPatInfo.cardtype,
		        carddata: cqPatInfo.carddata
		    };
		    if (cqPatInfo.ybVer == "shanghaiV5") {
		        $.ajax({
		            type: "POST",
		            url: "http://127.0.0.1:33333/api/FifthPhaseYiBao/ybInterface_SJ11",
		            dataType: "json",
		            data: ghxx,
		            async: false,
		            success: function (data) {
		                var medicalReg = eval('(' + data + ')');
		                if (medicalReg) {
		                    if (medicalReg.xxfhm === "P001" ) {
		                        jzid = medicalReg.xxnr.jzdyh;
		                        nextStepCallback(true);
		                    }
		                    else {
		                        $.modalAlert("门诊挂号失败：" + medicalReg.fhxx, 'error');
		                        $.loading(false);
		                        nextStepCallback(false);
		                    }
		                }
		            },
		            error: function (request, error, ex) {
		                $.modalAlert("医保服务【SJ11】(就诊登记业务)失败：[" + ex + "]", 'error');
		                $.loading(false);
		                nextStepCallback(false);
		            }
		        });
		    } else {
		        $.ajax({
		            type: "POST",
		            url: "http://127.0.0.1:33333/api/YiBao/Registered_2201",
		            dataType: "json",
		            data: ghxx,
		            async: false,
		            success: function (data) {
		                var medicalReg = eval('(' + data + ')');
		                if (medicalReg) {
		                    if (medicalReg.infcode == "0" && !!medicalReg.output.data) {
		                        jzid = medicalReg.output.data.mdtrt_id;
		                        nextStepCallback(true);
		                    }
		                    else {
		                        $.modalAlert("门诊挂号失败：" + medicalReg.err_msg, 'error');
		                        $.loading(false);
		                        nextStepCallback(false);
		                        return false;;
		                    }
		                }
		            }
		        });
		    }
			
		}, 50);
		return;
	}

	function funcStep5(nextStepCallback) {
		var patCardInfo = {
		    sfzh: cqPatInfo.sfzh,
		    kh: yibaoCardInfo.kh,//cqPatInfo.kh,
		    cblb: cqPatInfo.cblb,
		    jzid: jzid,
		    jzpzlx: dkfs,
		    xzlx: cqPatInfo.xzlx,
		    grbh: cqPatInfo.grbh,
		    cbdbm: cqPatInfo.xzqh,
		    ybVer:cqPatInfo.ybVer
		};
		$.najax({
			type: "POST",
			url: "/OutpatientManage/OutpatCharge/ZFToYB_Step_6",
			data: { mzh: mzh, patid: patid, patCardInfo: patCardInfo },
			success: function (data) {
				nextStepCallback(true);
			},
			errorCallback: function () {
			    $.loading(false);
				nextStepCallback(false);
			},
			error: function () {
			    $.loading(false);
				nextStepCallback(false);
			}
		});
		return;
	}

	function funcStep6(nextStepCallback) {
		$.najax({
		    url: "/OutpatientManage/OutpatCharge/ZFToYB_Step_8",
			data: { mzh: mzh },
			success: function (data) {
				nextStepCallback(true);
			},
			errorCallback: function () {
			    $.loading(false);
				nextStepCallback(false);
			},
			error: function () {
			    $.loading(false);
				nextStepCallback(false);
			}
		});
		return;
	}

	function funcStep7(nextStepCallback) {
		return true;
	}

	function shanghaiyb() {
	    var res = { "success": "true", "msg": "" };
	    var sm01Info;
	    var jzpzlx = dkfs;
	    var url = "http://127.0.0.1:33333/api/FifthPhaseYiBao/ybInterface_S000";
	    var payoptype = { "cardtype": "1", "operatorId": '@(opr.rygh)', "operatorName": '@(opr.UserName)', "businessType": "01101", "officeId": "0201", "officeName": "内科" };
	    if (jzpzlx == "0") {//磁条卡
	        payoptype.cardtype = "0";
	        payoptype.carddata = pzm;
	        url = "http://127.0.0.1:33333/api/FifthPhaseYiBao/ybInterface_SM01";
	    }
	    if (jzpzlx == "2") {//保障卡
	        payoptype.cardtype = "1";
	    }
	    if (jzpzlx == "3") {//电子凭证
	        payoptype.cardtype = "3";
	        payoptype.carddata = khxx.sbkh;
	        url = "http://127.0.0.1:33333/api/FifthPhaseYiBao/ybInterface_SE01";
	    }
	    if (jzpzlx == "4") {//身份证
	        payoptype.cardtype = "2";
	        payoptype.carddata = khxx.sbkh;
	        url = "http://127.0.0.1:33333/api/FifthPhaseYiBao/ybInterface_SM01";
	    }
	    if (jzpzlx == "1") {
	        res.success = false;
	        res.msg = "院内卡读卡接口未开通";
	    }
	    if (res.success) {
	        $.ajax({
	            type: "POST",
	            url: url,
	            dataType: "json",
	            data: payoptype,
	            async: false,
	            success: function (data) {
	                yibaoCardInfo = eval('(' + data + ')');
	            },
	            error: function (request, error, ex) {
	                res.success = false;
	                res.msg = "医保服务读卡不可访问：[" + ex + "]";
	            }
	        });
	        if (yibaoCardInfo) {
	            if (yibaoCardInfo.xxfhm === "P001") {
	                sm01Info = yibaoCardInfo.sm01;//账户信息(s000 、se01 、sm01)
	                cqPatInfo.xm = sm01Info.xm;
	                cqPatInfo.sfzh = sm01Info.sfzh;
	                cqPatInfo.xzqh = sm01Info.xzqh;
	                cqPatInfo.cblb = sm01Info.accountattr;
	                cqPatInfo.ecToken = sm01Info.ecToken;
	                cqPatInfo.sbkh = sm01Info.kh;
	                cqPatInfo.kh = sm01Info.kh;
	                cqPatInfo.accountattr = sm01Info.accountattr;
	                if (jzpzlx == "3") {
	                    cqPatInfo.carddata = sm01Info.ecToken;
	                }
	                else if (jzpzlx == "0" || jzpzlx == "4") {
	                    cqPatInfo.carddata = pzm;
	                }
	                else {
	                    cqPatInfo.carddata = "";
	                }
	                debugger;
	                var bkbz = sm01Info.accountattr.substr(11, 1);//帮困 G、A
	                var gbbz = sm01Info.accountattr.substr(1, 1);//干保 1
	                if (bkbz == "G" || bkbz == "A" || gbbz == "1") {
	                    cqPatInfo.ybVer = "shanghaiV5";
	                    cqPatInfo.cardtype = payoptype.cardtype;
	                    cqPatInfo.jzlx = jzpzlx;

	                    yibaoCardInfo.readCardCardType = jzpzlx;
	                    yibaoCardInfo.kh = sm01Info.kh;
	                    yibaoCardInfo.qtjz = jzpzlx != "1" ? cqPatInfo.sfzh : null;
	                    yibaoCardInfo.ybVer = "shanghaiV5";
	                    yibaoCardInfo.jzlx = jzpzlx;
	                }
	                else {
	                    return gjyb();
	                }
	            } else {
	                res.success = false;
	                res.msg = "医保服务(获取医保中心患者信息失败):" + yibaoCardInfo.fhxx;
	            }
	        } else {
	            res.success = false;
	            res.msg = "医保服务(获取医保中心未返回卡信息):";
	        }
	    }
	    return res;
	}
	function gjyb() {
	    var res = { "success": "true", "msg": "" };
	    setTimeout(function () {
	        //var payoptype = { "cardtype": "1", "operatorId": '@(opr.rygh)', "operatorName": '@(opr.UserName)', "businessType": "01101", "officeId": "0201", "officeName": "内科" };
	        var payoptype = { "mdtrt_cert_type": "02", "mdtrt_cert_no": cqPatInfo.sfzh, "operatorId": '@(opr.rygh)', "operatorName": '@(opr.UserName)', "businessType": "01101", "officeId": "0201", "officeName": "口腔科" };
	        var url = "http://127.0.0.1:33333/api/YiBao/CardecInfo_1161";
	        if (dkfs == "3") {//电子凭证
	            url = "http://127.0.0.1:33333/api/YiBao/CardecInfo_1162";
	            payoptype.mdtrt_cert_type = "01";
	            payoptype.mdtrt_cert_no = cqPatInfo.ecToken;
	        } else if (dkfs == "4")//身份证
	        {
	            url = "http://127.0.0.1:33333/api/YiBao/CardecInfo_1101";
	            payoptype.mdtrt_cert_type = "02";
	            payoptype.mdtrt_cert_no = pzm;
	        } else if (dkfs == "0")//医保磁卡
	        {
	            payoptype.mdtrt_cert_type = "00";
	            payoptype.mdtrt_cert_no = pzm;
	        }
	        $.ajax({
	            type: "POST",
	            url: url,
	            dataType: "json",
	            data: payoptype,
	            async: false,
	            success: function (data) {
	                yibaoCardInfo = eval('(' + data + ')');
	            }
	        });
	        $.loading(false);
	        if (yibaoCardInfo) {
	            if (yibaoCardInfo.infcode == "0") {
	                //var ybklx = "03";
	                cardInfo1 = yibaoCardInfo.output.baseinfo; //医保基本信息
	                cardInfo2 = yibaoCardInfo.output.insuinfo[0];//参保信息
	                if (dkfs !== "3") {
	                    cardInfo3 = yibaoCardInfo.output.cardecinfo;//卡信息
	                    cqPatInfo.sbkh = cqPatInfo.sbkh;
	                }
	                if (dkfs == "3") {
	                    cqPatInfo.ecToken = cardInfo3.ecToken;
	                }
	                cqPatInfo.xm = cardInfo1.psn_name;
	                cqPatInfo.sfzh = cardInfo1.certno;
	                cqPatInfo.xzqh = cardInfo2.insuplc_admdvs;
	                cqPatInfo.cblb = cardInfo2.psn_type;
	                cqPatInfo.xzlx = cardInfo2.insutype;
	                cqPatInfo.grbh = cardInfo1.psn_no;
	                cqPatInfo.jzlx = dkfs;
	                cqPatInfo.ybVer = "gjyb";

	                if (dkfs !== "4") {
	                    cardInfo3 = yibaoCardInfo.output.cardecinfo;//卡信息
	                    cardInfo1.ecToken = cardInfo3.ecToken;
	                    cardInfo1.sbkh = cardInfo3.cardno;
	                    cqPatInfo.ecToken = cardInfo3.ecToken;
	                    cqPatInfo.sbkh = cardInfo3.cardno;
	                }
	                yibaoCardInfo.readCardCardType = payoptype.mdtrt_cert_type;//ybkCardType;
	                yibaoCardInfo.kh = payoptype.mdtrt_cert_type == "01" ? cardInfo3.ecToken : (payoptype.mdtrt_cert_type == "02" ? cardInfo1.certno : cardInfo3.cardno);

	                //cqPatInfo.xm = cardInfo1.psn_name;
	                //cqPatInfo.sfzh = cardInfo1.certno;
	                //cqPatInfo.xzqh = cardInfo2.insuplc_admdvs;
	                //cqPatInfo.cblb = cardInfo2.psn_type;
	                //cqPatInfo.xzlx = cardInfo2.insutype;
	                //cqPatInfo.grbh = cardInfo1.psn_no;
	                //cqPatInfo.kh = yibaoCardInfo.kh;
	                //nextStepCallback(true);
	            } else {
	                res.success = false;
	                res.msg = "刷卡失败:" + yibaoCardInfo.err_ms;

	            }
	        } else {
	            res.success = false;
	            res.msg = "刷卡获取医保信息失败！失败原因：医保中心无信息返回，请重试！";
	        }
	    }, 50);
	    return res;
	}
</script>